package cn.gov.msa.pagehelper;

import java.util.Map;

import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;

import com.github.pagehelper.Page;
import com.github.pagehelper.parser.impl.AbstractParser;

public class CustomOracleParse extends AbstractParser {
 
	private static  String[] param ;
	public  void setParam(String[] params) {
		 param = params;
	}

	@Override
	public String getPageSql(String sql) {
		 	StringBuilder sqlBuilder = new StringBuilder(sql.length() + 120);
		 	sqlBuilder.append("SELECT tt2.* ");
		 	if(null!=param) {
			 	for (int i = 1; i < param.length; i++) {
			 		 sqlBuilder.append(","+param[i]);
				}
		 	}
	        sqlBuilder.append(" FROM ( SELECT tt1.*, ROWNUM XH from ( ");
	        sqlBuilder.append(sql);
	        sqlBuilder.append(" ) tt1 where rownum <= ? ) tt2 where XH > ?");
	        return sqlBuilder.toString();
	}
	@Override
	public Map<String, Object> setPageParameter(MappedStatement ms, Object parameterObject, BoundSql boundSql, Page<?> page) {
	        Map<String, Object> paramMap = super.setPageParameter(ms, parameterObject, boundSql, page);
	        paramMap.put(PAGEPARAMETER_FIRST, page.getEndRow());
	        paramMap.put(PAGEPARAMETER_SECOND, page.getStartRow());
	        return paramMap;
	}
}
